package com.yzy.others;


import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONType;
import com.alibaba.fastjson.parser.deserializer.*;
import com.alibaba.fastjson.serializer.*;
import com.alibaba.fastjson.util.ServiceLoader;
import com.alibaba.fastjson.util.TypeUtils;
import com.yzy.common.ListNode;

import java.lang.reflect.*;
import java.net.InetAddress;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.MessageDigest;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.*;

/**
 * Description:
 * Date: 2019-03-14
 *
 * @author youzhiyong
 */
public class Test {
    /*private static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }*/
    public static int findBestValue(int[] arr, int target) {
        int len = arr.length;
        if (len == 1) return target;
        int mid = getABSMid(len, target);
        Arrays.sort(arr);

        for (int i = 0; i < len - 1; i++) {
            if (arr[i] < mid) {
                target -= arr[i];
                mid = getABSMid(len - i - 1, target);
                continue;
            }
            break;
        }

        return Math.min(mid, arr[len - 1]);
    }

    public static int getABSMid(int len, int target) {
        int absMid = target / len;
        return (target - absMid * len) <= ((absMid + 1) * len - target) ? absMid : absMid + 1;
    }

    /**
     * 加密
     *
     * @param str
     * @return
     */
    /*public static String encode(String str) {
        try {

            // 此 MessageDigest 类为应用程序提供信息摘要算法的功能，必须用try,catch捕获
            MessageDigest md5 = MessageDigest.getInstance("MD5");

            // 输入的字符串转换成字节数组
            byte[] inputByteArray = str.getBytes();

            // inputByteArray是输入字符串转换得到的字节数组
            md5.update(inputByteArray);
            byte[] resultByteArray = md5.digest();

            // 字符数组转换成字符串返回
            return bytesToHexString(resultByteArray);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }*/
    public static void main(String[] args) {
/*
        ListNode head = new ListNode(0);
        head.next = new ListNode(1);
        head.next.next = new ListNode(2);
        head.next.next.next = new ListNode(3);
        head.next.next.next.next = new ListNode(4);
        head.next.next.next.next.next = new ListNode(5);

        System.out.println(head);

        if (head == null) return;
        ListNode newHead = head;
        head = head.next;
        newHead.next = null;

        while (head != null) {
            ListNode temp = head.next;
            head.next = newHead;
            newHead = head;
            head = temp;
        }

        System.out.println(newHead);*/

        //Long memberId = null;
        //System.out.println(Integer.parseInt(String.valueOf(memberId)));
        //System.out.println(findBestValue(new int[]{2,3,5}, 11));
        int[] nums = new int[]{1, 3, 2, 2, 3, 1};
        Arrays.sort(nums);
        int len = nums.length;
        int mid = len / 2 + len % 2;
        int j = len - 1;
        for (int i=1; i<len; i+=2) {
            int temp = nums[i];
            nums[i] = nums[j];
            nums[j] = temp;
            j--;
        }

    }


}
